package org.rayson.share.transport;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ByteChannel;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.SelectionKey;
import java.nio.channels.SocketChannel;

public class NioChannel implements ByteChannel {

	protected final SelectionKey sk;

	protected final SocketChannel sc;

	public NioChannel(final SelectionKey sk, final SocketChannel sc) {
		super();
		this.sk = sk;
		this.sc = sc;
	}

	@Override
	public void close() throws IOException {
		sc.close();
	}

	public int getInterestOps() {
		return sk.interestOps();
	}

	public InetSocketAddress getLocalAddress() {
		return null;
	}

	public InetSocketAddress getRemoteAddress() {
		return null;
	}

	public void interestOps(final int ops) {
		sk.interestOps(ops);
	}

	@Override
	public boolean isOpen() {
		return sc.isOpen();
	}

	@Override
	public int read(final ByteBuffer dst) throws IOException {
		return sc.read(dst);
	}

	public SelectionKey registerInterest(final int ops)
			throws ClosedChannelException {
		return sc.register(sk.selector(), ops);
	}

	public void wakeupSelector() {
		this.sk.selector().wakeup();
	}

	@Override
	public int write(final ByteBuffer src) throws IOException {
		return sc.write(src);
	}
}